package ca.unbc.cpsc672;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

import ca.unbc.cpsc672.rcmd.BusinessInfo;
import ca.unbc.cpsc672.rcmd.RcmdOptions;
import ca.unbc.cpsc672.rcmd.RcommandationGenerator;

public class RcmdProgressBar extends Dialog {
	private BusinessInfo[] infoAry;
	private RcmdOptions options;

	public RcmdProgressBar(Shell parentShell, RcmdOptions options) {
		super(parentShell);
		this.options = options;
	}

	@Override
	protected Control createContents(final Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout());
		composite.setLayoutData(GUIUtils.buildGridData());

		ProgressBar pb = new ProgressBar(composite.getShell(), SWT.HORIZONTAL
				| SWT.INDETERMINATE);
		pb.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		new Thread() {
			@Override
			public void run() {
				try {
					infoAry = new RcommandationGenerator(
							options).run();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				parent.getDisplay().asyncExec(new Runnable() {
					public void run() {
						RcmdProgressBar.this.close();
					}
				});
			}
		}.start();

		return super.createContents(parent);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Analyzing");
	}

	@Override
	protected Button createButton(Composite parent, int id, String label,
			boolean defaultButton) {
		return null;
	}

	public BusinessInfo[] getResult() {
		return infoAry;
	}
}